/*
package com.snap.vseries.fragment.cit;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;


import com.snap.vseries.R;
import com.snap.vseries.base.CitFragment;
import com.snap.vseries.bean.TestItem;
import com.snap.vseries.dialog.NumberInputDialog;
import com.snap.vseries.framework.MoleExecutor;
import com.snap.vseries.framework.MoleTask;
import com.snap.vseries.serial.Cmd;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.ToastUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.Future;

import butterknife.BindView;



@SuppressLint("ValidFragment")
public class TempStabilityFragment extends CitFragment {

    private static final int DEFAULT_COUNT = 30;
    private static final float DEFAULT_OFFSET = 0.3f;

    @BindView(R.id.temp_et)
    TextView offsetEt;
    @BindView(R.id.count_et)
    TextView countEt;
    @BindView(R.id.result_label)
    TextView resultLabel;
    @BindView(R.id.result_tv)
    TextView resultTv;
    @BindView(R.id.start_btn)
    TextButton startBtn;

    private TestItem testItem;
    private final int position;

    private int count = DEFAULT_COUNT;
    private float offset = DEFAULT_OFFSET;
    private Future future;
    private long cancelClickTime;

    public TempStabilityFragment(int position, TestItem testItem) {
        this.position = position;
        this.testItem = testItem;
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_cit_temp_stability;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        offsetEt.setText(String.valueOf(offset));
        offsetEt.setOnClickListener(v -> {
            new NumberInputDialog().setParam(1, 0, offset, 2, "误差范围", "°C", value -> {
                offset = value;
                offsetEt.setText(String.valueOf(offset));
            }).show(getChildFragmentManager(), "");
        });
        countEt.setText(String.valueOf(count));
        countEt.setOnClickListener(v ->
                new NumberInputDialog().setParam(0, 1, count, 120, "采样次数", "", value -> {
                    count = (int) value;
                    countEt.setText(String.valueOf(count));
                }).show(getChildFragmentManager(), ""));
        startBtn.setOnClickListener(v -> {
            if (count == 0) return;
            if (!isTesting()) {
                setTesing(true);
                startBtn.setText("测试中...(双击取消测试)");
                startBtn.setBackgroundResource(R.drawable.btn_cit_running);
                resultLabel.setVisibility(View.GONE);
                resultTv.setText("");
                future = MoleExecutor.getInstance().execute(new MoleTask<Boolean>() {
                    @Override
                    public Boolean runInBackground() throws InterruptedException {
                        try {
                            getSerialPortHelper().open();
                            int[] targetTempArr = {37, 55, 72, 95};
                            for (int i = 0; i < targetTempArr.length; i++) {
                                final int targetTemp = targetTempArr[i];

                                //设置温度
                                Bundle bundle = getSerialPortHelper().execute(Cmd.setTemp(DEVICE_TYPE, 1, targetTempArr[i] * 100));
                                if (!bundle.getBoolean("suc")) {
                                    resultTv.post(() -> resultTv.append("设置温度" + targetTemp + "°C失败\n"));
                                    return false;
                                } else {
                                    resultTv.post(() -> resultTv.append("开始测试" + targetTemp + "°C温度稳定性\n"));
                                }

                                float[] tempArr = new float[count];
                                //等待温度变化
                                int holdTime = 0;
                                int holdSecs = 0;
                                while (holdTime < REQUIRE_HOLD_TIME) {
                                    Thread.sleep(1000);
                                    if (++holdSecs > 60) {
                                        resultTv.post(() -> resultTv.append("无法达到" + targetTemp + "°C,测试失败\n"));
                                        return false;
                                    }
                                    bundle = getSerialPortHelper().execute(Cmd.readTemp(DEVICE_TYPE, 1));
                                    if (bundle.getBoolean("suc")) {
                                        byte[] data = bundle.getByteArray("data");
                                        if (data != null && data.length > 9) {
                                            float tecTemp = (((data[8] & 0xff) << 8) + (data[9] & 0xff)) / 100f;
                                            if (Math.abs(tecTemp - targetTemp) <= offset) {
                                                tempArr[holdTime++] = tecTemp;
                                            } else if (holdTime > 0) {
                                                holdTime = 0;
                                            }
                                        }
                                    }
                                }

                                for (int j = REQUIRE_HOLD_TIME; j < count; j++) {
                                    Thread.sleep(1000);
                                    bundle = getSerialPortHelper().execute(Cmd.readTemp(DEVICE_TYPE, 1));
                                    if (bundle.getBoolean("suc")) {
                                        byte[] data = bundle.getByteArray("data");
                                        if (data != null && data.length > 9) {
                                            float tecTemp = (((data[8] & 0xff) << 8) + (data[9] & 0xff)) / 100f;
                                            tempArr[j] = tecTemp;
                                        }
                                    }
                                }

                                resultTv.post(() -> resultTv.append(targetTemp + "°C稳定性：" + Arrays.toString(tempArr)));
                                Thread.sleep(1000);
                            }
                            return true;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return false;
                        } finally {
                            try {
                                getSerialPortHelper().execute(Cmd.power(DEVICE_TYPE, 1, false));
                            } catch (Exception e) {

                            }
                            getSerialPortHelper().close();
                        }
                    }

                    @Override
                    public void onCompleted(Boolean result) {
                        if (result) {
                            testItem.setState(1);
                            testItem.setComment("通过");
                        } else {
                            testItem.setState(2);
                            testItem.setComment("失败");
                        }
                        setTesing(false);
                        startBtn.setText("开始测试");
                        startBtn.setBackgroundResource(R.drawable.btn_cit_start);
                        resultLabel.setVisibility(View.VISIBLE);
                        resultLabel.setBackgroundResource(testItem.getState() == 2 ? R.drawable.bg_red_4 : R.drawable.bg_blue6_4);
                        notifyItem(position);
                    }
                });
            } else {
                if ((System.currentTimeMillis() - cancelClickTime) > 2000) {
                    cancelClickTime = System.currentTimeMillis();
                } else {
                    if (future != null) future.cancel(true);
                    ToastUtils.get().showText("已取消！");
                    setTesing(false);
                    startBtn.setText("开始测试");
                    startBtn.setBackgroundResource(R.drawable.btn_cit_start);
                    resultLabel.setVisibility(View.GONE);
                    resultTv.setText("");

                    testItem.setState(0);
                    testItem.setComment("");
                    notifyItem(position);
                }
            }
        });
    }
}
*/
